package com.cim120.commons.cip.process.strategy;

import com.cim120.commons.cip.constant.ImageConstant;
import com.cim120.commons.cip.constant.ImageEnum;
import com.cim120.commons.cip.model.Image;
import com.cim120.commons.cip.utils.GraphicsMagickUtils;
import com.cim120.commons.utils.FileUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by tonydeng on 16/4/27.
 */
public class HmpPortraitCropStrategy extends CropStrategy {
    private static final Logger log = LoggerFactory.getLogger(HmpPortraitCropStrategy.class);

    @Override
    public boolean crop(ImageEnum.Type type, List<File> files) {
        boolean status = true;
        if(CollectionUtils.isNotEmpty(files)){
           files.stream().forEach(
                   f -> {
                       GraphicsMagickUtils.resizeByfixedWidthAndHeight(
                               f.getPath(),
                               FileUtil.getFilePathName(f, ImageEnum.Flag.SMALL.getValue()),
                               ImageConstant.HMP_PRORTRAIT_SMALL_WIDTH,
                               ImageConstant.HMP_PRORTRAIT_SMALL_WIDTH
                       );
                       GraphicsMagickUtils.resizeByfixedWidthAndHeight(
                               f.getPath(),
                               FileUtil.getFilePathName(f, ImageEnum.Flag.MIDDLE.getValue()),
                               ImageConstant.HMP_PRORTRAIT_MIDDLE_WIDTH,
                               ImageConstant.HMP_PRORTRAIT_MIDDLE_WIDTH
                       );
                       GraphicsMagickUtils.resizeByfixedWidthAndHeight(
                               f.getPath(),
                               FileUtil.getFilePathName(f, ImageEnum.Flag.LARGE.getValue()),
                               ImageConstant.HMP_PRORTRAIT_LARGE_WIDTH,
                               ImageConstant.HMP_PRORTRAIT_LARGE_WIDTH
                       );
                   }
           );
        }
        return status;
    }

    @Override
    public Map<String, Image> getResultMap(ImageEnum.Type type, Map<String, Image> results) {
        if(results != null){
            Image orig = results.get(ImageEnum.Flag.ORIGIN.getValue());
            if(orig != null){
                results.put(ImageEnum.Flag.SMALL.getValue(), getImageInfoByOrigin(orig, ImageEnum.Flag.SMALL.getValue()));
                results.put(ImageEnum.Flag.MIDDLE.getValue(), getImageInfoByOrigin(orig, ImageEnum.Flag.MIDDLE.getValue()));
                results.put(ImageEnum.Flag.LARGE.getValue(), getImageInfoByOrigin(orig, ImageEnum.Flag.LARGE.getValue()));
            }
        }
        return results;
    }
}
